﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Text;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class WaveBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDa = DataAccess.CreateOrdersDA();
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private static readonly IPickDetailDA pickDetailDA = DataAccess.CreatePickDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly IShipmentContainerDA shipmentContainerDA = DataAccess.CreateShipmentContainerDA();
        private static readonly IWaveDA waveDa = DataAccess.CreateWaveDA();
        private static readonly IWaveDetailDA waveDetailDa = DataAccess.CreateWaveDetailDA();

        public WaveBLL()
        {
            this.pubParasInfo = null;
        }

        public WaveBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public WaveBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string BatchClosed(List<string> waveID)
        {
            string strtemp = string.Empty;
            List<WaveDetailInfo> waveDetailInfo = null;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; waveID.Count > i; i++)
                {
                    waveDa.UpdateWaveStatus(dataBase, tran, waveID[i], OrderStatus.Closed, this.pubParasInfo.UserID, this.dataBaseID[0]);
                    waveDetailInfo = waveDetailDa.GetWaveDetailListByID(dataBase, tran, waveID[i], this.dataBaseID[0]);
                    if ((waveDetailInfo != null) && (waveDetailInfo.Count > 0))
                    {
                        waveDetailDa.UpdateWaveDetailStatusByWaveID(dataBase, tran, waveID[i], OrderStatus.Closed, this.pubParasInfo.UserID, this.dataBaseID[0]);
                        foreach (WaveDetailInfo info in waveDetailInfo)
                        {
                            if (((info.ProcessStatus != OrderStatus.New) && (info.ProcessStatus != OrderStatus.Shipped)) && !(info.ProcessStatus == OrderStatus.PartShipped))
                            {
                                throw new Exception("出库单:" + info.OrderID + "状态不等于新建 或者 部分发货 或者 发货 , 改单据无法关闭!");
                            }
                            ordersDa.UpdateOrdersStatus(dataBase, tran, info.OrderID, OrderStatus.Closed, this.pubParasInfo.UserID, this.dataBaseID[0]);
                            orderDetailDA.UpdateOrderDetailStatus(dataBase, tran, info.OrderID, OrderStatus.Closed, this.pubParasInfo.UserID, this.dataBaseID[0]);
                        }
                    }
                    strtemp = strtemp + waveID[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strtemp;
        }

        public void BatchConfirmPicked(string waveID)
        {
            List<WaveDetailInfo> waveDetailInfo = new List<WaveDetailInfo>();
            try
            {
                waveDetailInfo = waveDetailDa.GetWaveDetailListByID(waveID, this.dataBaseID[0]);
                if ((waveDetailInfo != null) && (waveDetailInfo.Count > 0))
                {
                    foreach (WaveDetailInfo info in waveDetailInfo)
                    {
                        List<PickDetailInfo> pickDetailInfo = pickDetailDA.GetPickDetailByOrderID(info.OrderID, this.dataBaseID[0], this.pubParasInfo);
                        if ((pickDetailInfo != null) && (pickDetailInfo.Count > 0))
                        {
                            List<string> pickDetailID = new List<string>();
                            foreach (PickDetailInfo pickinfo in pickDetailInfo)
                            {
                                if ((pickinfo.Status.ToUpper() == "NEW") || (pickinfo.Status.ToUpper() == "RELEASED"))
                                {
                                    pickDetailID.Add(pickinfo.PickDetailID);
                                }
                            }
                            if (pickDetailID.Count > 0)
                            {
                                new OutboundMgtBLL(this.pubParasInfo).ConfirmPicked(pickDetailID, null, this.pubParasInfo.UserID);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
        }

        public void BatchShipping(string waveID)
        {
            List<WaveDetailInfo> waveDetailInfo = new List<WaveDetailInfo>();
            try
            {
                waveDetailInfo = waveDetailDa.GetWaveDetailListByID(waveID, this.dataBaseID[0]);
                if ((waveDetailInfo != null) && (waveDetailInfo.Count > 0))
                {
                    foreach (WaveDetailInfo info in waveDetailInfo)
                    {
                        List<PickDetailInfo> pickDetailInfo = pickDetailDA.GetPickDetailByOrderID(info.OrderID, this.dataBaseID[0], this.pubParasInfo);
                        if ((pickDetailInfo != null) && (pickDetailInfo.Count > 0))
                        {
                            List<string> pickDetailID = new List<string>();
                            foreach (PickDetailInfo pickinfo in pickDetailInfo)
                            {
                                if (pickinfo.Status.ToUpper() == "PICKED")
                                {
                                    pickDetailID.Add(pickinfo.PickDetailID);
                                }
                            }
                            if (pickDetailID.Count > 0)
                            {
                                OutboundMgtBLL outboundMgtBLL = new OutboundMgtBLL(this.pubParasInfo);
                                string strshippingDate = DateTime.Now.ToString();
                                outboundMgtBLL.ConfirmShipped(pickDetailID, strshippingDate, this.pubParasInfo.UserID);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
        }

        public string DeleteWave(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<WaveInfo> waveInfos = new List<WaveInfo>();
                WaveInfo waveInfo = null;
                foreach (string id in IDS)
                {
                    waveInfo = waveDa.GetWaveByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    waveInfo.WaveDetailInfo = waveDetailDa.GetWaveDetailListByID(dataBase, tran, id, this.dataBaseID[0]);
                    waveInfos.Add(waveInfo);
                    if ((waveInfo.WaveDetailInfo != null) && (waveInfo.WaveDetailInfo.Count > 0))
                    {
                        i = 0;
                        while (i < waveInfo.WaveDetailInfo.Count)
                        {
                            if (waveInfo.WaveDetailInfo[i].OrderLineID != 0)
                            {
                                orderDetailDA.UpdateOrderDetailBatchFlag(dataBase, tran, "N", waveInfo.WaveDetailInfo[i].OrderID, waveInfo.WaveDetailInfo[i].OrderLineID.ToString(), this.dataBaseID[0]);
                            }
                            else
                            {
                                ordersDa.UpdateOrderBatchFlag(dataBase, tran, "N", waveInfo.WaveDetailInfo[i].OrderID, this.dataBaseID[0]);
                                break;
                            }
                            i++;
                        }
                    }
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                waveDetailDa.DeleteWaveDetail(dataBase, tran, IDS, this.dataBaseID[0]);
                if (waveDa.DeleteWave(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<WaveInfo>(dataBase, tran, waveInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "WAVE", this.dataBaseID[0]);
                        if ((waveInfos[i].WaveDetailInfo != null) && (waveInfos[i].WaveDetailInfo.Count > 0))
                        {
                            for (int j = 0; j < waveInfos[i].WaveDetailInfo.Count; j++)
                            {
                                auditDataBLL.InsertAuditData<WaveDetailInfo>(dataBase, tran, waveInfos[i].WaveDetailInfo[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "WAVE_DETAIL", this.dataBaseID[0]);
                            }
                        }
                        temp = temp + IDS[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllWaveInfo()
        {
            return waveDa.GetAllWave(this.dataBaseID[0], this.pubParasInfo);
        }

        public List<OrderDetailInfo> GetBatchOrdersInfoAndDetailListByID(string waveID, string ContainerID)
        {
            List<WaveDetailInfo> waveDetailList = waveDetailDa.GetWaveDetailListByID(waveID, this.dataBaseID[0]);
            List<OrderDetailInfo> orderDetailList = new List<OrderDetailInfo>();
            if ((waveDetailList != null) && (waveDetailList.Count > 0))
            {
                foreach (WaveDetailInfo info in waveDetailList)
                {
                    OrdersInfo ordersInfo = new OrdersInfo {
                        OrderDetailInfos = orderDetailDA.GetOrderDetailListByOrderIDAndContainerID(info.OrderID, ContainerID, this.dataBaseID[0], this.pubParasInfo),
                        ShipmentContainerInfos = shipmentContainerDA.GetShipmentContainerListByOrderID(info.OrderID, ContainerID, this.dataBaseID[0])
                    };
                    InboundHelper inboundHelper = new InboundHelper();
                    OutboundMgtBLL outBoundMgtBll = new OutboundMgtBLL();
                    if ((ordersInfo.OrderDetailInfos != null) && (ordersInfo.OrderDetailInfos.Count > 0))
                    {
                        Dictionary<string, PackInfo> packInfoList = new Dictionary<string, PackInfo>();
                        for (int i = 0; i < ordersInfo.OrderDetailInfos.Count; i++)
                        {
                            PackInfo packInfo = null;
                            if (packInfoList.ContainsKey(ordersInfo.OrderDetailInfos[i].PackID))
                            {
                                packInfo = packInfoList[ordersInfo.OrderDetailInfos[i].PackID];
                            }
                            else
                            {
                                packInfo = packDA.GetPackByID(ordersInfo.OrderDetailInfos[i].PackID, this.dataBaseID[0]);
                                packInfoList.Add(ordersInfo.OrderDetailInfos[i].PackID, packInfo);
                            }
                            ordersInfo.OrderDetailInfos[i].OriginalQty = inboundHelper.ExchangeUom(packInfo, ordersInfo.OrderDetailInfos[i].UomID, ordersInfo.OrderDetailInfos[i].OriginalQty, "R");
                            ordersInfo.OrderDetailInfos[i].OpenQty = inboundHelper.ExchangeUom(packInfo, ordersInfo.OrderDetailInfos[i].UomID, ordersInfo.OrderDetailInfos[i].OpenQty, "R");
                            ordersInfo.OrderDetailInfos[i].ShippedQty = inboundHelper.ExchangeUom(packInfo, ordersInfo.OrderDetailInfos[i].UomID, ordersInfo.OrderDetailInfos[i].ShippedQty, "R");
                            ordersInfo.OrderDetailInfos[i].AdjustedQty = inboundHelper.ExchangeUom(packInfo, ordersInfo.OrderDetailInfos[i].UomID, ordersInfo.OrderDetailInfos[i].AdjustedQty, "R");
                            ordersInfo.OrderDetailInfos[i].PreallocatedQty = inboundHelper.ExchangeUom(packInfo, ordersInfo.OrderDetailInfos[i].UomID, ordersInfo.OrderDetailInfos[i].PreallocatedQty, "R");
                            ordersInfo.OrderDetailInfos[i].AllocatedQty = inboundHelper.ExchangeUom(packInfo, ordersInfo.OrderDetailInfos[i].UomID, ordersInfo.OrderDetailInfos[i].AllocatedQty, "R");
                            ordersInfo.OrderDetailInfos[i].PickedQty = inboundHelper.ExchangeUom(packInfo, ordersInfo.OrderDetailInfos[i].UomID, ordersInfo.OrderDetailInfos[i].PickedQty, "R");
                            orderDetailList.Add(ordersInfo.OrderDetailInfos[i]);
                        }
                    }
                }
            }
            return orderDetailList;
        }

        public string GetOrderIDsByWaveID(string waveID, PubParasInfo pubParasInfo)
        {
            return waveDa.GetOrderIDsByWaveID(waveID, this.dataBaseID[0], pubParasInfo);
        }

        public DataSet GetOrdersByOrderSelection(OrderSelectionInfo OrderSelection)
        {
            StringBuilder StrSql = new StringBuilder();
            IOrderSelectionDA OrderSelectionDA = DataAccess.CreateOrderSelectionDA();
            DataSet ds = null;
            if (OrderSelection == null)
            {
                return ds;
            }
            if (!string.IsNullOrEmpty(OrderSelection.OrderIdStart) || !string.IsNullOrEmpty(OrderSelection.OrderIdEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.OrderIdStart) || string.IsNullOrEmpty(OrderSelection.OrderIdEnd)))
                {
                    StrSql.AppendFormat(" AND A.ORDER_ID BETWEEN '{0}' AND '{1}'  ", OrderSelection.OrderIdStart, OrderSelection.OrderIdEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.OrderIdStart))
                {
                    StrSql.AppendFormat(" AND A.ORDER_ID >='{0}' ", OrderSelection.OrderIdStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.ORDER_ID <='{0}' ", OrderSelection.OrderIdEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.OwnerIdStart) || !string.IsNullOrEmpty(OrderSelection.OwnerIdEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.OwnerIdStart) || string.IsNullOrEmpty(OrderSelection.OwnerIdEnd)))
                {
                    StrSql.AppendFormat(" AND A.OWNER_ID BETWEEN '{0}' AND '{1}'  ", OrderSelection.OwnerIdStart, OrderSelection.OwnerIdEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.OwnerIdStart))
                {
                    StrSql.AppendFormat(" AND  A.OWNER_ID >='{0}' ", OrderSelection.OwnerIdStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.ORDER_ID <='{0}' ", OrderSelection.OwnerIdEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.OrderStatusStart) || !string.IsNullOrEmpty(OrderSelection.OrderStatusEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.OrderStatusStart) || string.IsNullOrEmpty(OrderSelection.OrderStatusEnd)))
                {
                    StrSql.AppendFormat(" AND A.STATUS BETWEEN '{0}' AND '{1}'  ", OrderSelection.OrderStatusStart, OrderSelection.OrderStatusEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.OrderStatusStart))
                {
                    StrSql.AppendFormat(" AND  A.STATUS >='{0}' ", OrderSelection.OrderStatusStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.STATUS <='{0}' ", OrderSelection.OrderStatusEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.OrderTypeStart) || !string.IsNullOrEmpty(OrderSelection.OrderTypeEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.OrderTypeStart) || string.IsNullOrEmpty(OrderSelection.OrderTypeEnd)))
                {
                    StrSql.AppendFormat(" AND A.TYPE BETWEEN '{0}' AND '{1}'  ", OrderSelection.OrderTypeStart, OrderSelection.OrderTypeEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.OrderTypeStart))
                {
                    StrSql.AppendFormat(" AND  A.TYPE >='{0}' ", OrderSelection.OrderTypeStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.TYPE <='{0}' ", OrderSelection.OrderTypeEnd);
                }
            }
            DateTime CS00002 = new DateTime();
            if ((OrderSelection.OrderDateStart != CS00002) || (OrderSelection.OrderDateEnd != (CS00002 = new DateTime())))
            {
                CS00002 = new DateTime();
                if ((OrderSelection.OrderDateStart != CS00002) && (OrderSelection.OrderDateEnd != (CS00002 = new DateTime())))
                {
                    StrSql.AppendFormat(" AND A.ORDER_DATE BETWEEN '{0}' AND '{1}'  ", OrderSelection.OrderDateStart, OrderSelection.OrderDateEnd);
                }
                else
                {
                    CS00002 = new DateTime();
                    if (OrderSelection.OrderDateStart != CS00002)
                    {
                        StrSql.AppendFormat(" AND  A.ORDER_DATE >='{0}' ", OrderSelection.OrderDateStart);
                    }
                    else
                    {
                        StrSql.AppendFormat(" AND  A.ORDER_DATE <='{0}' ", OrderSelection.OrderDateEnd);
                    }
                }
            }
            CS00002 = new DateTime();
            if ((OrderSelection.DeliveryDateStart != CS00002) || (OrderSelection.DeliveryDateEnd != (CS00002 = new DateTime())))
            {
                CS00002 = new DateTime();
                if ((OrderSelection.DeliveryDateStart != CS00002) && (OrderSelection.DeliveryDateEnd != (CS00002 = new DateTime())))
                {
                    StrSql.AppendFormat(" AND A.DELIVERY_DATE BETWEEN '{0}' AND '{1}'  ", OrderSelection.DeliveryDateStart, OrderSelection.DeliveryDateEnd);
                }
                else if (OrderSelection.DeliveryDateStart != new DateTime())
                {
                    StrSql.AppendFormat(" AND  A.DELIVERY_DATE >='{0}' ", OrderSelection.DeliveryDateStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.DELIVERY_DATE <='{0}' ", OrderSelection.DeliveryDateEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.ExternOrderIdStart) || !string.IsNullOrEmpty(OrderSelection.ExternOrderIdEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.ExternOrderIdStart) || string.IsNullOrEmpty(OrderSelection.ExternOrderIdEnd)))
                {
                    StrSql.AppendFormat(" AND A.EXTERN_ORDER_ID BETWEEN '{0}' AND '{1}'  ", OrderSelection.ExternOrderIdStart, OrderSelection.ExternOrderIdEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.ExternOrderIdStart))
                {
                    StrSql.AppendFormat(" AND  A.EXTERN_ORDER_ID >='{0}' ", OrderSelection.ExternOrderIdStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.EXTERN_ORDER_ID <='{0}' ", OrderSelection.ExternOrderIdEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.OrderPriorityStart) || !string.IsNullOrEmpty(OrderSelection.OrderPriorityEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.OrderPriorityStart) || string.IsNullOrEmpty(OrderSelection.OrderPriorityEnd)))
                {
                    StrSql.AppendFormat(" AND A.PRIORITY BETWEEN '{0}' AND '{1}'  ", OrderSelection.OrderPriorityStart, OrderSelection.OrderPriorityEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.OrderPriorityStart))
                {
                    StrSql.AppendFormat(" AND  A.PRIORITY >='{0}' ", OrderSelection.OrderPriorityStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.PRIORITY <='{0}' ", OrderSelection.OrderPriorityEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.CompanyStart) || !string.IsNullOrEmpty(OrderSelection.CompanyEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.CompanyStart) || string.IsNullOrEmpty(OrderSelection.CompanyEnd)))
                {
                    StrSql.AppendFormat(" AND A.CONSIGNEE_ID BETWEEN '{0}' AND '{1}'  ", OrderSelection.OrderPriorityStart, OrderSelection.CompanyEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.CompanyStart))
                {
                    StrSql.AppendFormat(" AND  A.CONSIGNEE_ID >='{0}' ", OrderSelection.CompanyStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.CONSIGNEE_ID <='{0}' ", OrderSelection.CompanyEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.CityBegin) || !string.IsNullOrEmpty(OrderSelection.CityEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.CityBegin) || string.IsNullOrEmpty(OrderSelection.CityEnd)))
                {
                    StrSql.AppendFormat(" AND A.C_CITY BETWEEN '{0}' AND '{1}'  ", OrderSelection.CityBegin, OrderSelection.CityEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.CityBegin))
                {
                    StrSql.AppendFormat(" AND  A.C_CITY >='{0}' ", OrderSelection.CityBegin);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.C_CITY <='{0}' ", OrderSelection.CityEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.CarrierIdStart) || !string.IsNullOrEmpty(OrderSelection.CarrierIdEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.CarrierIdStart) || string.IsNullOrEmpty(OrderSelection.CarrierIdEnd)))
                {
                    StrSql.AppendFormat(" AND A.CARRIER_ID BETWEEN '{0}' AND '{1}'  ", OrderSelection.CarrierIdStart, OrderSelection.CarrierIdEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.CarrierIdStart))
                {
                    StrSql.AppendFormat(" AND  A.CARRIER_ID >='{0}' ", OrderSelection.CarrierIdStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.CARRIER_ID <='{0}' ", OrderSelection.CarrierIdEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.DoorStart) || !string.IsNullOrEmpty(OrderSelection.DoorEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.DoorStart) || string.IsNullOrEmpty(OrderSelection.DoorEnd)))
                {
                    StrSql.AppendFormat(" AND A.DOOR BETWEEN '{0}' AND '{1}'  ", OrderSelection.DoorStart, OrderSelection.DoorEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.DoorStart))
                {
                    StrSql.AppendFormat(" AND  A.DOOR >='{0}' ", OrderSelection.DoorStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.DOOR <='{0}' ", OrderSelection.DoorEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.RouteStart) || !string.IsNullOrEmpty(OrderSelection.RouteEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.RouteStart) || string.IsNullOrEmpty(OrderSelection.RouteEnd)))
                {
                    StrSql.AppendFormat(" AND A.ROUTE BETWEEN '{0}' AND '{1}'  ", OrderSelection.DoorStart, OrderSelection.RouteEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.RouteStart))
                {
                    StrSql.AppendFormat(" AND  A.ROUTE >='{0}' ", OrderSelection.RouteStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.ROUTE <='{0}' ", OrderSelection.RouteEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.StopStart) || !string.IsNullOrEmpty(OrderSelection.StopEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.StopStart) || string.IsNullOrEmpty(OrderSelection.StopEnd)))
                {
                    StrSql.AppendFormat(" AND A.STOP BETWEEN '{0}' AND '{1}'  ", OrderSelection.StopStart, OrderSelection.StopEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.StopStart))
                {
                    StrSql.AppendFormat(" AND  A.STOP >='{0}' ", OrderSelection.StopStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.STOP <='{0}' ", OrderSelection.StopEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.LoadIdStart) || !string.IsNullOrEmpty(OrderSelection.LoadIdEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.LoadIdStart) || string.IsNullOrEmpty(OrderSelection.LoadIdEnd)))
                {
                    StrSql.AppendFormat(" AND A.LOAD_ID BETWEEN '{0}' AND '{1}'  ", OrderSelection.LoadIdStart, OrderSelection.LoadIdEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.LoadIdStart))
                {
                    StrSql.AppendFormat(" AND  A.LOAD_ID >='{0}' ", OrderSelection.LoadIdStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  A.LOAD_ID <='{0}' ", OrderSelection.LoadIdEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.SkuIdStart) || !string.IsNullOrEmpty(OrderSelection.SkuIdEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.SkuIdStart) || string.IsNullOrEmpty(OrderSelection.SkuIdEnd)))
                {
                    StrSql.AppendFormat(" AND B.SKU_ID BETWEEN '{0}' AND '{1}'  ", OrderSelection.SkuIdStart, OrderSelection.SkuIdEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.SkuIdStart))
                {
                    StrSql.AppendFormat(" AND  B.SKU_ID >='{0}' ", OrderSelection.SkuIdStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  B.SKU_ID <='{0}' ", OrderSelection.SkuIdEnd);
                }
            }
            if (!string.IsNullOrEmpty(OrderSelection.LotStart) || !string.IsNullOrEmpty(OrderSelection.LotEnd))
            {
                if (!(string.IsNullOrEmpty(OrderSelection.LotStart) || string.IsNullOrEmpty(OrderSelection.LotEnd)))
                {
                    StrSql.AppendFormat(" AND B.LOT BETWEEN '{0}' AND '{1}'  ", OrderSelection.LotStart, OrderSelection.LotEnd);
                }
                else if (!string.IsNullOrEmpty(OrderSelection.LotStart))
                {
                    StrSql.AppendFormat(" AND  B.LOT >='{0}' ", OrderSelection.LotStart);
                }
                else
                {
                    StrSql.AppendFormat(" AND  B.LOT <='{0}' ", OrderSelection.LotEnd);
                }
            }
            return waveDa.GetOrdersByOrderSelection(StrSql.ToString(), this.dataBaseID[0], this.pubParasInfo);
        }

        public WaveInfo GetWaveByID(string id)
        {
            WaveInfo wave = waveDa.GetWaveByID(id, this.dataBaseID[0], this.pubParasInfo);
            wave.OrdersInfo = new List<OrdersInfo>();
            if (wave != null)
            {
                int i;
                wave.WaveDetailInfo = waveDetailDa.GetWaveDetailListByID(id, this.dataBaseID[0]);
                if ((wave.WaveDetailInfo == null) || (wave.WaveDetailInfo.Count <= 0))
                {
                    return wave;
                }
                if (wave.WaveDetailInfo[0].OrderLineID == 0)
                {
                    for (i = 0; wave.WaveDetailInfo.Count > i; i++)
                    {
                        OrdersInfo ordersinfo = ordersDa.GetOrdersByID(wave.WaveDetailInfo[i].OrderID, this.dataBaseID[0], this.pubParasInfo);
                        if (ordersinfo != null)
                        {
                            wave.ShipmentContainerInfos = shipmentContainerDA.GetShipmentContainerByOrderID(ordersinfo.OrderID, this.dataBaseID[0]);
                            wave.OrdersInfo.Add(ordersinfo);
                        }
                    }
                    return wave;
                }
                wave.OrderDetailsInfo = new List<OrderDetailInfo>();
                for (i = 0; wave.WaveDetailInfo.Count > i; i++)
                {
                    OrderDetailInfo orderDetail = orderDetailDA.GetOrderDetailByID(wave.WaveDetailInfo[i].OrderID, wave.WaveDetailInfo[i].OrderLineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                    wave.OrderDetailsInfo.Add(orderDetail);
                }
            }
            return wave;
        }

        public DataSet GetWaveByQueryList(WaveQueryEntity waveQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(waveQuery.WaveID))
            {
                sqlWhere.Add(" W.WAVE_ID " + waveQuery.WaveID);
            }
            if (!string.IsNullOrEmpty(waveQuery.ExternalWaveID))
            {
                sqlWhere.Add(" W.EXTERNAL_WAVE_ID" + waveQuery.ExternalWaveID);
            }
            if (!string.IsNullOrEmpty(waveQuery.Status))
            {
                sqlWhere.Add(" W.STATUS " + waveQuery.Status);
            }
            if (!string.IsNullOrEmpty(waveQuery.Descr))
            {
                sqlWhere.Add(" W.DESCR " + waveQuery.Descr);
            }
            if (!string.IsNullOrEmpty(waveQuery.CreatedBy))
            {
                sqlWhere.Add(" W.CREATED_BY " + waveQuery.CreatedBy);
            }
            return waveDa.GetWaveByQueryList(sqlWhere, waveQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string GetWaveStatus(List<WaveDetailInfo> waveDetailInfos)
        {
            string status = OrderStatus.New;
            if ((waveDetailInfos != null) && (waveDetailInfos.Count > 0))
            {
                int totalCount = waveDetailInfos.Count;
                int New = 0;
                int PartPreAllocated = 0;
                int PreAllocated = 0;
                int PartAllocated = 0;
                int Allocated = 0;
                int PartPicked = 0;
                int Picked = 0;
                int PartShipped = 0;
                int Shipped = 0;
                int Reviewed = 0;
                for (int i = 0; i < waveDetailInfos.Count; i++)
                {
                    string tmpStatus = waveDetailInfos[i].ProcessStatus.ToUpper();
                    if (tmpStatus == OrderStatus.New.ToUpper())
                    {
                        New++;
                    }
                    if (tmpStatus == OrderStatus.PartPreAllocated.ToUpper())
                    {
                        PartPreAllocated++;
                    }
                    if (tmpStatus == OrderStatus.PreAllocated.ToUpper())
                    {
                        PreAllocated++;
                    }
                    if (tmpStatus == OrderStatus.PartAllocated.ToUpper())
                    {
                        PartAllocated++;
                    }
                    if (tmpStatus == OrderStatus.Allocated.ToUpper())
                    {
                        Allocated++;
                    }
                    if (tmpStatus == OrderStatus.PartPicked.ToUpper())
                    {
                        PartPicked++;
                    }
                    if (tmpStatus == OrderStatus.Picked.ToUpper())
                    {
                        Picked++;
                    }
                    if (tmpStatus == OrderStatus.Reviewed.ToUpper())
                    {
                        Reviewed++;
                    }
                    if (tmpStatus == OrderStatus.PartShipped.ToUpper())
                    {
                        PartShipped++;
                    }
                    if (tmpStatus == OrderStatus.Shipped.ToUpper())
                    {
                        Shipped++;
                    }
                }
                if (New == 0)
                {
                    status = OrderStatus.New;
                }
                if (PartPreAllocated > 0)
                {
                    status = OrderStatus.PartPreAllocated;
                }
                if ((PreAllocated > 0) && (PreAllocated < totalCount))
                {
                    status = OrderStatus.PartPreAllocated;
                }
                if (PreAllocated == totalCount)
                {
                    status = OrderStatus.PreAllocated;
                }
                if (PartAllocated > 0)
                {
                    status = OrderStatus.PartAllocated;
                }
                if ((Allocated > 0) && (Allocated < totalCount))
                {
                    status = OrderStatus.PartAllocated;
                }
                if (Allocated == totalCount)
                {
                    status = OrderStatus.Allocated;
                }
                if (PartPicked > 0)
                {
                    status = OrderStatus.PartPicked;
                }
                if ((Picked > 0) && (Picked < totalCount))
                {
                    status = OrderStatus.PartPicked;
                }
                if (Picked == totalCount)
                {
                    status = OrderStatus.Picked;
                }
                if ((Reviewed > 0) && (Reviewed < totalCount))
                {
                    status = OrderStatus.PartReviewed;
                }
                if (Reviewed == totalCount)
                {
                    status = OrderStatus.Reviewed;
                }
                if (PartShipped > 0)
                {
                    status = OrderStatus.PartShipped;
                }
                if ((Shipped > 0) && (Shipped < totalCount))
                {
                    status = OrderStatus.PartShipped;
                }
                if (Shipped == totalCount)
                {
                    status = OrderStatus.Shipped;
                }
            }
            return status;
        }

        public string InsertWave(WaveInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(obj.WaveID))
                {
                    obj.WaveID = commonMethod.GenNextNumStr(dataBase, tran, "WAVE", this.dataBaseID[0]);
                }
                obj.UpdatedBy = obj.CreatedBy;
                obj.CreatedDate = DateTime.Now;
                obj.UpdatedDate = DateTime.Now;
                if (waveDa.CheckWaveIDUnique(dataBase, tran, obj.WaveID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F7300_001", this.pubParasInfo.UserLanguage, new List<string> { obj.WaveID });
                }
                if (waveDa.InsertWave(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<WaveInfo>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "WAVE", this.dataBaseID[0]);
                    strWaveID = obj.WaveID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveID;
        }

        public string UpdateWave(WaveInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveID = string.Empty;
            try
            {
                WaveInfo oldWaveInfo = waveDa.GetWaveByID(dataBase, tran, obj.WaveID, this.dataBaseID[0], this.pubParasInfo);
                if (waveDa.UpdateWave(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<WaveInfo>(oldWaveInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "WAVE", this.dataBaseID[0]);
                    strWaveID = obj.WaveID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveID;
        }

        public string UpdateWaveStatusByOrderID(string OrderID)
        {
            string waveStatus = string.Empty;
            WaveDetailInfo waveDetailinfo = waveDetailDa.GetWaveDetailByOrderID(OrderID, this.dataBaseID[0]);
            if (waveDetailinfo != null)
            {
                List<WaveDetailInfo> waveDetailInfos = waveDetailDa.GetWaveDetailListByID(waveDetailinfo.WaveID, this.dataBaseID[0]);
                if ((waveDetailInfos != null) && (waveDetailInfos.Count > 0))
                {
                    waveStatus = this.GetWaveStatus(waveDetailInfos);
                    waveDa.UpdateWaveStatus(waveDetailinfo.WaveID, waveStatus, this.pubParasInfo.UserID, this.dataBaseID[0]);
                }
            }
            return waveStatus;
        }

        public string UpdateWaveStatusByOrderID(DataBase dataBase, DbTransaction tran, string OrderID)
        {
            string waveStatus = string.Empty;
            WaveDetailInfo waveDetailinfo = waveDetailDa.GetWaveDetailByOrderID(dataBase, tran, OrderID, this.dataBaseID[0]);
            if (waveDetailinfo != null)
            {
                List<WaveDetailInfo> waveDetailInfos = waveDetailDa.GetWaveDetailListByID(dataBase, tran, waveDetailinfo.WaveID, this.dataBaseID[0]);
                if ((waveDetailInfos != null) && (waveDetailInfos.Count > 0))
                {
                    waveStatus = this.GetWaveStatus(waveDetailInfos);
                    waveDa.UpdateWaveStatus(dataBase, tran, waveDetailinfo.WaveID, waveStatus, this.pubParasInfo.UserID, this.dataBaseID[0]);
                }
            }
            return waveStatus;
        }

        public string UpdateWaveStatusByWaveID(string waveID)
        {
            string waveStatus = string.Empty;
            List<WaveDetailInfo> waveDetailInfos = waveDetailDa.GetWaveDetailListByID(waveID, this.dataBaseID[0]);
            if ((waveDetailInfos != null) && (waveDetailInfos.Count > 0))
            {
                waveStatus = this.GetWaveStatus(waveDetailInfos);
                waveDa.UpdateWaveStatus(waveID, waveStatus, this.pubParasInfo.UserID, this.dataBaseID[0]);
            }
            return waveStatus;
        }

        public int WaveStatusConvert(string strStatus)
        {
            switch (strStatus)
            {
                case "New":
                    return 10;

                case "PartPre-Allocated":
                    return 20;

                case "Pre-Allocated":
                    return 30;

                case "PartAllocated":
                    return 40;

                case "Allocated":
                    return 50;

                case "Released":
                    return 0x37;

                case "PartPicked":
                    return 60;

                case "Picked":
                    return 70;

                case "InLoading":
                    return 80;

                case "Loaded":
                    return 90;

                case "PartShipped":
                    return 100;

                case "Shipped":
                    return 110;

                case "Cancelled":
                    return 120;

                case "Closed":
                    return 130;

                case "Scheduled":
                    return 140;
            }
            return 0;
        }
    }
}

